#include <Rectangle.h>
#include <RectCalc.h>
#include <Range.h>
#include <Precision.h>

namespace 
{
#define MIN_VALUE 0
#define MAX_LENGTH_VALUE 100
#define MAX_WIDTH_VALUE  75

LopenRcloseRange lenRange(MIN_VALUE, MAX_LENGTH_VALUE);
LopenRopenRange widRange(MIN_VALUE, MAX_WIDTH_VALUE);

}

Range* Rectangle::lenRange_m = &lenRange;
Range* Rectangle::widRange_m = &widRange;

////////////////////////////////////////////////////////////////////////////
/////////////////// CLASS FUNCTION ////////////////////////////////////////

Rectangle::Rectangle(const double length_i, const double width_i):
    length_m(Precision::cut(length_i)),
    width_m(Precision::cut(width_i))
{
}


bool Rectangle::isValid() const
{
    return (lenRange_m->isValid(length_m) && widRange_m->isValid(width_m));
}

double Rectangle::calcArea() const
{
    double area = RectCalc::calcArea(length_m, width_m);
    return (isValid() ?  Precision::round(area) : 0);
}

double Rectangle::calcPerimeter() const
{
    double perimeter = RectCalc::calcPerimeter(length_m, width_m);
    return (isValid() ? Precision::round(perimeter) : 0);
}

void Rectangle::setLength(const double& length_i)
{
    double temp = Precision::cut(length_i);
    if (lenRange_m->isValid(temp))
        length_m = temp;
}

void Rectangle::setWidth(const double& width_i)
{
    double temp = Precision::cut(width_i);
    if (widRange_m->isValid(temp))
        width_m = temp;
}


